//
//  shuixiaDetailViewController.m
//  UI_HLJ_Frame
//
//  Created by  毕宏伟 on 15/7/7.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "shuixiaDetailViewController.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#import "AFNetworking.h"
#import "shuixiaDetailModel.h"
#import "shuixiamerchantModel.h"
#import "shuixiazidingyiCellCollectionViewCell.h"
#import "shuixiataocanPageModel.h"
#import "shuixiataocanPagemerchantModel.h"
@interface shuixiaDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UICollectionView *collectionView;

@property(nonatomic,retain)NSMutableArray *onePageArr;

@property(nonatomic,retain)NSMutableArray *twoPageArr;

@property(nonatomic,retain)NSMutableArray *merchantArr;

@property(nonatomic,retain)NSMutableArray *merchanttwoArr;

@property(nonatomic,retain)UISegmentedControl *segmentControl;

//@property(nonatomic,retain)NSIndexPath *index;
@end

@implementation shuixiaDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    
    [self createData];
    [self createView];
    
    
}


- (void)createView
{
    UICollectionViewFlowLayout *flow =[[UICollectionViewFlowLayout alloc]init];
    flow.itemSize =CGSizeMake(WIDTH, HEIGHT);
    flow.sectionInset =UIEdgeInsetsMake(0, 0, 0, 0);
    flow.minimumInteritemSpacing =0;
    flow.minimumLineSpacing =0;
    [flow setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    
    //创建collectionView
    self.collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, WIDTH, HEIGHT) collectionViewLayout:flow];
    self.collectionView.delegate =self;
    self.collectionView.dataSource =self;
    self.collectionView.backgroundColor =[UIColor whiteColor];
    self.collectionView.pagingEnabled =YES;
    [_collectionView release];
    [self.collectionView registerClass:[shuixiazidingyiCellCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    [self.view addSubview:self.collectionView];
    
    
    //创建seg
    
    //创建一个数组
    NSArray *arr =[NSArray arrayWithObjects:@"套餐",@"案例", nil];
    
    self.segmentControl =[[UISegmentedControl alloc]initWithItems:arr];
    
    self.segmentControl.frame =CGRectMake(self.view.frame.size.width/3, 10, 100, 30);
    self.segmentControl.selectedSegmentIndex =0;
    [self.view addSubview:self.segmentControl];
    [self.segmentControl addTarget:self action:@selector(click:) forControlEvents:UIControlEventValueChanged];
    
    
    
}



#pragma mark 实现collectionview的协议方法

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
    
}

- (void)click:(UISegmentedControl *)button
{
    
    if (self.segmentControl.selectedSegmentIndex==0) {
        self.collectionView.contentOffset =CGPointMake(0, 0);
    }
    else
    {
        self.collectionView.contentOffset =CGPointMake(WIDTH, 0);
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    shuixiazidingyiCellCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
//    cell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//    [UIView animateWithDuration:0.5 animations:^{
//        
//        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//    }];
    if (indexPath.row==0) {
        //self.segmentControl.selectedSegmentIndex =indexPath.row;
        if (self.onePageArr) {
            
            cell.onepageArr =self.onePageArr;
            cell.merchantArr =self.merchantArr;
            cell.flag =indexPath.row;
            [cell.tableView reloadData];
        }
    }
    else
    {
        if (self.twoPageArr) {
            //self.segmentControl.selectedSegmentIndex =indexPath.row;
            cell.twoPageArr =self.twoPageArr;
            cell.merchanttwoArr = self.merchanttwoArr;
            cell.flag =indexPath.row;
            [cell.tableView reloadData];
        }
    }
    
    
    
    
    
    return cell;
}




- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.collectionView.contentOffset.x ==0) {
        
        self.segmentControl.selectedSegmentIndex =0;
    
    }
    else
    {
        self.segmentControl.selectedSegmentIndex =1;
    }
    
   }



#pragma mark 解析数据
- (void)createData
{
    self.onePageArr = [NSMutableArray array];
    self.twoPageArr = [NSMutableArray array];
    self.merchantArr = [NSMutableArray array];
    self.merchanttwoArr =[NSMutableArray array];
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    NSString *str =[NSString stringWithFormat:@"http://hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList/page/1/per_page/20?commodity_type=1&first_query_time=2015-07-07T08%%3A01%%3A56Z&mark=%@&sort%%5Bkey%%5D=default&sort%%5Border%%5D=desc",self.idurl];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dic =responseObject;
        NSDictionary *worksArr =dic[@"works"];
        for (NSDictionary *worksDic in worksArr) {
            
            shuixiaDetailModel *shui =[[shuixiaDetailModel alloc]init];
            [shui setValuesForKeysWithDictionary:worksDic];
            [self.onePageArr addObject:shui];
            [shui release];
            
            NSDictionary *merchantdic =worksDic[@"merchant"];
            shuixiamerchantModel *mer = [[shuixiamerchantModel alloc]init];
            [mer setValuesForKeysWithDictionary:merchantdic];
            [self.merchantArr addObject:mer];
            [mer release];

            
        }
        
       
        [self.collectionView reloadData];
        
        NSLog(@"数据解析成功");
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
    AFHTTPRequestOperationManager *manager1 =[AFHTTPRequestOperationManager manager];
    
    [manager1 GET:[NSString stringWithFormat:@"http://hunliji.com/p/wedding/index.php/home/APIMerchant/WorkList/page/1/per_page/20?commodity_type=0&first_query_time=2015-07-08T01%%3A55%%3A38Z&mark=%@&sort%%5Bkey%%5D=default&sort%%5Border%%5D=desc",self.idurl] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        NSDictionary *dic =responseObject;
        NSArray *worksArr =dic[@"works"];
        for (NSDictionary *worksDic in worksArr) {
            shuixiataocanPageModel *shui =[[shuixiataocanPageModel alloc]init];
            [shui setValuesForKeysWithDictionary:worksDic];
            [self.twoPageArr addObject:shui];
            [shui release];
            
            NSDictionary *merchantdic =worksDic[@"merchant"];
            shuixiataocanPagemerchantModel *mer =[[shuixiataocanPagemerchantModel alloc]init];
            
            [mer setValuesForKeysWithDictionary:merchantdic];
            
            [self.merchanttwoArr addObject:mer];
            [mer release];
            
        }
        
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
    }];
    
    
    
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
